package cn.j0n4than.accountingtool.controller.api.user;

import cn.j0n4than.accountingtool.controller.common.BaseController;
import cn.j0n4than.accountingtool.entity.User;
import cn.j0n4than.accountingtool.pojo.Auth;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * 用户控制器基类
 *
 * @author jonathan
 */
public class UserBaseController extends BaseController {

    /**
     * 获取当前请求的User
     * 有时候只是不想看到idea警告
     *
     * @return User
     */
    protected User getCurrentUser() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        assert requestAttributes != null;
        HttpServletRequest request = requestAttributes.getRequest();
        Auth auth = (Auth) request.getAttribute("auth");
        return userService.getUserById(auth.userId);
    }
}
